#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int f[N], w[N];
int t, n;

// f[i][j] = max(f[i - 1][j] , f[i - 1][j - v] + w)
int main()
{
    cin >> t >> n;
    for (int i = 1; i <= n; ++i)
    {
        int t1, w1;
        cin >> t1 >> w1;
        for (int j = t; j >= t1; --j)
            f[j] = max(f[j], f[j - t1] + w1);
    }
    cout << f[t] << endl;
    return 0;
}